package com.itheima.reggie.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    //套餐列表
    @Override
    public Page<Setmeal> findList(Integer pageNum, Integer pageSize, String name) {

        //1. 分页查询套餐信息
        //1-1 设置分页条件
        Page<Setmeal> page = new Page<>();
        //1-2 设置业务条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName, name);
        setmealMapper.selectPage(page, queryWrapper);
        List<Setmeal> records = page.getRecords();
        //1-3 执行查询
        //2. 变量列表,得到每个套餐
        if (CollectionUtil.isNotEmpty(records)) {
            for (Setmeal setmeal : records) {
                //3. 根据套餐的category_id去category表中获取分类名称
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
                //4. 根据套餐id去setmeal_dish中查询套餐信息
                LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
                List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
                setmeal.setSetmealDishes(setmealDishes);
            }
        }
        return page;
    }
//添加套餐
    @Override
    public void save(Setmeal setmeal) {
        //保存套餐
        setmealMapper.insert(setmeal);
        //获取菜品
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            int i = 0;
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDish.setSort(i++);

            }
        }
    }
//批量删除
    @Override
    public void deleteById(List<Long> ids) {
        //查询正在售卖的套餐
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getStatus, 1)
                .in(Setmeal::getId, ids);
        Integer count = setmealMapper.selectCount(queryWrapper);
        if (count>0) {
            throw new CustomException("尚有套餐在售卖,不可删除");
        }
        //删除菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper);
        //删除套餐
        setmealMapper.deleteBatchIds(ids);
    }
//套餐回显
    @Override
    public Setmeal findById(Long id) {
        //根据id从套餐表中获取信息
        Setmeal setmeal = setmealMapper.selectById(id);

        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishes);
        return setmeal;
    }
//修改套餐
    @Override
    public void update(Setmeal setmeal) {
        setmealMapper.updateById(setmeal);
        //删除旧的菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);
        //添加新的菜品
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            int i = 0;
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDish.setSort(i++);
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
//批停售
    @Override
    public void updateStstus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealMapper.update(setmeal, wrapper);
    }
//套餐显示
    @Override
    public List<Setmeal> findSetmealList(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, status);
      return setmealMapper.selectList(queryWrapper);

    }
//查询套餐菜品
    @Override
    public List<Dish> findDishList(Long id) {
        //获取套餐内的菜品集合
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(queryWrapper);
        List<Dish> dishList = new ArrayList<Dish>();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                Dish dish = dishMapper.selectById(setmealDish.getDishId());
                dish.setCopies(setmealDish.getCopies());
                dishList.add(dish);
            }
        }
        return dishList;
    }


}
